package cn.edu.suda.schoolBus.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.suda.schoolBus.bean.AdminUser;
import cn.edu.suda.schoolBus.service.AdminUserService;
import cn.edu.suda.schoolBus.util.ResultUtil;

@Controller
public class AdminUserController {

	@Autowired
	private AdminUserService adminUserService;

	@RequestMapping("/addAdminUser")
	@ResponseBody
	public Map<String, Boolean> addAdminUser(AdminUser adminUser) {
		return ResultUtil.getResult(adminUserService.addAdminUser(adminUser));
	}

	@RequestMapping("/getAdminUser")
	@ResponseBody
	public Map<String, Object> getAdminUser(int page, int rows) {
		return adminUserService.getAdminUser(page, rows);
	}

	@RequestMapping("/getAdminUserByAccount")
	@ResponseBody
	public Map<String, Boolean> getAdminUser(String auAccount) {
		return ResultUtil.getResult(adminUserService.getAdminUser(auAccount));
	}

	@RequestMapping("/deleteAdminUser")
	@ResponseBody
	public Map<String, Boolean> deleteAdminUser(String ids) {
		return ResultUtil.getResult(adminUserService.deleteAdminUser(ids));
	}

	@RequestMapping("/editAdminUser")
	@ResponseBody
	public Map<String, Boolean> editAdminUser(AdminUser adminUser) {
		return ResultUtil.getResult(adminUserService.editAdminUser(adminUser));
	}

	@RequestMapping("/adminUserLogin")
	@ResponseBody
	public Map<String, Boolean> adminUserLogin(HttpServletRequest request, String username, String passwd) {
		int i = 0;
		AdminUser adminUser = adminUserService.getAdminUser(username, passwd);
		if (adminUser != null) {
			i = 1;
			request.getSession().setAttribute("adminUserId", adminUser.getAuId());
			request.getSession().setAttribute("adminUserAccount", adminUser.getAuAccount());
			request.getSession().setAttribute("adminUserPasswd", adminUser.getAuPasswd());
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = simpleDateFormat.format(adminUser.getAuTime());
			request.getSession().setAttribute("adminUserTime", time);
		}
		return ResultUtil.getResult(i);
	}

	@RequestMapping("/getAdminUserInfo")
	@ResponseBody
	public Map<String, String> getAdminUserAccount(HttpServletRequest request) {
		String account = (String) request.getSession().getAttribute("adminUserAccount");
		String time = (String) request.getSession().getAttribute("adminUserTime");
		Map<String, String> map = new HashMap<>();
		map.put("account", account);
		map.put("time", time);
		return map;
	}

	@RequestMapping("/confirmAdminUserPasswd")
	@ResponseBody
	public Map<String, Object> confirmAdminUserPasswd(HttpServletRequest request, String passwd) {
		String adminPasswd = (String) request.getSession().getAttribute("adminUserPasswd");
		Map<String, Object> map = new HashMap<>();
		if (adminPasswd.trim().equals(passwd.trim())) {
			map.put("flag", true);
			map.put("adminUserId", request.getSession().getAttribute("adminUserId"));
		} else {
			map.put("flag", false);
		}
		return map;
	}

	@RequestMapping("/adminUserLogout")
	@ResponseBody
	public Map<String, Boolean> adminUserLogout(HttpServletRequest request) {
		int i = 0;
		request.getSession().invalidate();
		String account = (String) request.getSession().getAttribute("adminUserAccount");
		if (account == null) {
			i = 1;
		}
		return ResultUtil.getResult(i);
	}

}
